/*
 * Copyright (C) 2016 Jared Rummler
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.jaredrummler.materialspinner;

import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.List;

public abstract class MaterialSpinnerBaseAdapter<T> extends BaseItemProvider {

  private Context context;
  private int selectedIndex;
  private Color textColor;
  private int backgroundSelector;
  private int popupPaddingTop;
  private int popupPaddingLeft;
  private int popupPaddingBottom;
  private int popupPaddingRight;
  private boolean isHintEnabled;

  private PopupCustomDialog mDialog;
  private MaterialSpinnerAdapterWrapper.InternalListCallback mCallback;

  public MaterialSpinnerBaseAdapter(Context context) {
    this.context = context;
  }

  public MaterialSpinnerBaseAdapter(PopupCustomDialog dialog, Context context) {
    mDialog = dialog;
    this.context = context;
  }

  void setCallback(MaterialSpinnerAdapterWrapper.InternalListCallback callback) {
    mCallback = callback;
  }

  @Override
  public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
    if (component == null) {
      component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_ms__list_item, componentContainer, false);
    }
    onPrepareView(position, component);
    return component;
  }

  private void onPrepareView(int index, Component component) {
    Text title = (Text) component.findComponentById(ResourceTable.Id_tv_tinted_spinner);
    try {
      title.setBackground(ElementScatter.getInstance(context).parse(backgroundSelector));
    } catch (Exception e){
        e.printStackTrace();
    }
    title.setTextColor(textColor);
    title.setText((String) getItem(index));
    title.setPadding(popupPaddingLeft, popupPaddingTop, popupPaddingRight, popupPaddingBottom);
    component.setClickedListener(view -> {
      onItemClicked(view, index);
    });
  }

  private void onItemClicked(Component component, int index) {
    notifyItemSelected(index);
    if (mCallback != null) {
      CharSequence text = null;
      if (mDialog.builder.items != null) {
        text = mDialog.builder.items.get(index);
      }
      mCallback.onItemSelected(mDialog, component, index, text, false);
    }
  }

  interface InternalListCallback {
    boolean onItemSelected(PopupCustomDialog dialog, Component itemView, int position, CharSequence text, boolean longPress);
  }

  public String getItemText(int position) {
    return getItem(position).toString();
  }

  public int getSelectedIndex() {
    return selectedIndex;
  }

  public void notifyItemSelected(int index) {
    selectedIndex = index;
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public abstract T getItem(int position);

  @Override public abstract int getCount();

  public abstract T get(int position);

  public abstract List<T> getItems();

  public boolean isHintEnabled() {
    return this.isHintEnabled;
  }

  public MaterialSpinnerBaseAdapter<T> setCustomTextColor(Color textColor) {
    this.textColor = textColor;
    return this;
  }

  public MaterialSpinnerBaseAdapter<T> setBackgroundSelector(int backgroundSelector) {
    this.backgroundSelector = backgroundSelector;
    return this;
  }

  public MaterialSpinnerBaseAdapter<T> setPopupPadding(int left, int top, int right, int bottom) {
    this.popupPaddingLeft = left;
    this.popupPaddingTop = top;
    this.popupPaddingRight = right;
    this.popupPaddingBottom = bottom;
    return this;
  }

  private static class ViewHolder {

    private Text textView;

    private ViewHolder(Text textView) {
      this.textView = textView;
    }
  }
}